package com.open.capacity.form.util;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
public class KingbaseDbOperate {
    /**
     * 备份数据库db
     *
     * @param root
     * @param pwd
     * @param dbName
     * @param backPath
     * @param backName
     */
    public static void dbBackUp(String dbPath, String root, String pwd, String dbName, String backPath, String backName) throws Exception {
        File path = new File(backPath);
        if (!path.exists()) {
            path.mkdirs();
        }
        String pathSql = backPath + "/db_bak/" + backName;
        File path2 = new File(backPath + "//db_bak/");
        if (!path2.exists()) {
            path2.mkdirs();
        }
        File fileSql = new File(pathSql);
        //创建备份sql文件
        if (!fileSql.exists()) {
            fileSql.createNewFile();
        }
        //sys_dump -h 127.0.0.1 -p 54321 -U SYSTEM -W SYSTEM -f G:\2020\bak\dumpfile.dmp finance
        StringBuffer sb = new StringBuffer();
        sb.append("\"" + dbPath + "\\sys_dump\"");
        sb.append(" -h 127.0.0.1");
        sb.append(" -p 54321");
        sb.append(" -U " + root);
        sb.append(" -W " + pwd);
        sb.append(" -v -f " + pathSql);
        sb.append(" -F c " + dbName + " ");
        log.info("cmd命令为：" + sb.toString());
        Runtime runtime = Runtime.getRuntime();
        log.info("开始备份：" + dbName);
        Process process = runtime.exec("cmd /c" + sb.toString());
        Thread.sleep(10000);
        log.info("备份成功!");
    }

    /**
     * 恢复数据库
     *
     * @param root
     * @param pwd
     * @param dbName
     * @param filePath mysql -hlocalhost -uroot -p123456 db < /home/back.sql
     */
    public static void dbRestore(String dbPath, String root, String pwd, String dbName, String filePath) {
        StringBuilder sb = new StringBuilder();
        sb.append("\"" + dbPath + "\\ksql.exe\"");
        sb.append(" -h127.0.0.1");
        sb.append(" -U" + root);
        sb.append(" -W" + pwd);
        sb.append(" -d" + dbName);
        sb.append(" -p 54321");
        sb.append(" -f " + filePath);
        log.info("cmd命令为：" + sb.toString());
        Runtime runtime = Runtime.getRuntime();
        log.info("开始还原数据");
        try {
            Process process = runtime.exec("cmd /c" + sb.toString());
            Thread.sleep(10000);
//            InputStream is = process.getInputStream();
//            BufferedReader bf = new BufferedReader(new InputStreamReader(is, "utf8"));
//            String line = null;
//            while ((line = bf.readLine()) != null) {
//                log.info(line);
//            }
//            is.close();
//            bf.close();
        } catch (IOException | InterruptedException e) {
            log.error(e.toString());
        }
        log.info("还原成功！");
    }

    public static void main(String[] args) throws Exception {
        String backName = new SimpleDateFormat("20200101").format(new Date()) + ".sql";
        KingbaseDbOperate.dbBackUp("", "root", "251314", "finance", "E:/", backName);
        //dbRestore("root","251314","db","E://20200101.sql");
    }
}
